package com.dnake.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;

import com.dnake.Constant;
import com.dnake.entity.EventSettingBean;
import com.dnake.panel.R;
import com.dnake.utils.NetUtil;
import com.wx.android.common.util.SharedPreferencesUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.Bind;
import butterknife.ButterKnife;
import me.yokeyword.fragmentation.SupportFragment;

/**
 * @author haoyuliu
 * @since 2017/7/18
 */

public class SettingFragment extends SupportFragment {
    @Bind(R.id.rb_one)
    RadioButton rbOne;
    @Bind(R.id.rb_two)
    RadioButton rbTwo;
    @Bind(R.id.rb_app_one)
    RadioButton rbAppOne;
    @Bind(R.id.rb_app_two)
    RadioButton rbAppTwo;
    @Bind(R.id.rb_net_one)
    RadioButton rbNetOne;
    @Bind(R.id.rb_net_two)
    RadioButton rbNetTwo;
    private View mView;

    public static SettingFragment newInstance() {
        return new SettingFragment();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_setting, container, false);
        ButterKnife.bind(this, mView);
        if (SharedPreferencesUtils.getBoolean(Constant.SP_VIDEO_STATUS)) {
            rbOne.setChecked(true);
        } else {
            rbTwo.setChecked(true);
        }
        if (SharedPreferencesUtils.getBoolean(Constant.SP_DHCP_STATUS)) {
            rbNetOne.setChecked(true);
        } else {
            rbNetTwo.setChecked(true);
        }
        if (SharedPreferencesUtils.getInt(Constant.SP_APP_TYPE) == 0) {
            rbAppOne.setChecked(true);
        } else if (SharedPreferencesUtils.getInt(Constant.SP_APP_TYPE) == 1) {
            rbAppTwo.setChecked(true);
        }
        return mView;
    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public boolean onBackPressedSupport() {
        return super.onBackPressedSupport();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventSetting(EventSettingBean eventBusBean) {
        if (eventBusBean.getCode() == Constant.EVENTBUS_SETTING) {
            if ("1".equals(eventBusBean.getName())) {
                if (SharedPreferencesUtils.getBoolean(Constant.SP_VIDEO_STATUS)) {
                    rbTwo.setChecked(true);
                    SharedPreferencesUtils.put(Constant.SP_VIDEO_STATUS, false);
                } else {
                    rbOne.setChecked(true);
                    SharedPreferencesUtils.put(Constant.SP_VIDEO_STATUS, true);
                }
            } else if ("2".equals(eventBusBean.getName())) {
                if (SharedPreferencesUtils.getInt(Constant.SP_APP_TYPE) == 1) {
                    rbAppOne.setChecked(true);
                    SharedPreferencesUtils.put(Constant.SP_APP_TYPE, 0);
                } else if (SharedPreferencesUtils.getInt(Constant.SP_APP_TYPE) == 0) {
                    rbAppTwo.setChecked(true);
                    SharedPreferencesUtils.put(Constant.SP_APP_TYPE, 1);
                }
            } else if ("3".equals(eventBusBean.getName())) {
                start(FaceScoreFragment.newInstance());
            } else if ("4".equals(eventBusBean.getName())) {
                start(LockTypeFragment.newInstance());
            } else if ("5".equals(eventBusBean.getName())) {
                start(SyncFragment.newInstance());
            } else if ("6".equals(eventBusBean.getName())) {
                start(VoipPhoneFragment.newInstance());
            } else if ("7".equals(eventBusBean.getName())) {
                start(InformationFragment.newInstance());
            }else if ("8".equals(eventBusBean.getName())){
                start(VoiceFragment.newInstance());
//                if (SharedPreferencesUtils.getBoolean(Constant.SP_DHCP_STATUS)) {
//                    rbNetTwo.setChecked(true);
//                    SharedPreferencesUtils.put(Constant.SP_DHCP_STATUS, false);
//                } else {
//                    rbNetOne.setChecked(true);
//                    NetUtil.setNetwork();
//                    SharedPreferencesUtils.put(Constant.SP_DHCP_STATUS, true);
//                }
            }
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
